<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px"
    >
      <el-form-item label="企业ID" prop="tenantId">
        <!--        <el-input v-model="dataForm.tenantId" placeholder="企业ID"></el-input>-->
        <el-select v-model="dataForm.tenantId" placeholder="请选择">
          <el-option
            :disabled="dataForm.id ?true:false"
            v-for="item in firmInfos"
            :key="item.id"
            :label="item.eiName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="契约锁配置企业名称" prop="tenantName">
        <el-input v-model="dataForm.tenantName" placeholder="契约锁内部配置企业名称"></el-input>
      </el-form-item>
      <el-form-item label="密钥key" prop="accessKey">
        <el-input v-model="dataForm.accessKey" placeholder="契约锁密钥key"></el-input>
      </el-form-item>
      <el-form-item label="密钥" prop="accessSecret">
        <el-input v-model="dataForm.accessSecret" placeholder="契约锁密钥"></el-input>
      </el-form-item>
      <el-form-item label="印章ID" prop="sealId">
        <el-input onkeyup="this.value = this.value.replace(/[^\d.]/g,'')" v-model="dataForm.sealId" placeholder="印章ID"
        ></el-input>
      </el-form-item>
      <el-form-item label="业务ID" prop="categoryId">
        <el-input v-model="dataForm.categoryId" placeholder="业务分类ID"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="dataForm.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { geteSignature, SaveSignature, getFirmByJSFRole } from '@/api/SystemManagerApi'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        accessKey: '',
        tenantName: '',
        accessSecret: '',
        tenantId: '',
        sealId: '',
        categoryId: '',
        state: '',
        status: ''
      },
      firmInfos: {},
      dataRule: {
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '修改时间不能为空', trigger: 'blur' }
        ],
        accessKey: [
          { required: true, message: '契约锁密钥key不能为空', trigger: 'blur' },
          { max: 50, message: '长度不超过50个字符', trigger: 'blur' }
        ],
        tenantName: [
          { required: true, message: '契约锁内部配置企业名称不能为空', trigger: 'blur' },
          { max: 100, message: '长度不超过100个字符', trigger: 'blur' }
        ],
        accessSecret: [
          { required: true, message: '契约锁密钥不能为空', trigger: 'blur' }
        ],
        tenantId: [
          { required: true, message: '企业ID不能为空', trigger: 'blur' }
        ],
        sealId: [
          { required: true, message: '印章ID不能为空', trigger: 'blur' },
          { max: 19, message: '长度不超过19个字符', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '业务分类ID不能为空', trigger: 'blur' },
          { max: 19, message: '长度不超过19个字符', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '状态  1 可用 0 不可用不能为空', trigger: 'blur' }
        ],
        isDeleted: [
          { required: true, message: '逻辑删除  1 已删除  0 未删除不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getFirmByJSFRole()
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          geteSignature(this.dataForm.id).then((data) => {
            if (data && data.code === 20000) {
              this.dataForm.createTime = data.data.createTime
              this.dataForm.updateTime = data.data.updateTime
              this.dataForm.accessKey = data.data.accessKey
              this.dataForm.accessSecret = data.data.accessSecret
              this.dataForm.tenantId = data.data.tenantId
              this.dataForm.tenantName = data.data.tenantName
              this.dataForm.sealId = data.data.sealId
              this.dataForm.categoryId = data.data.categoryId
              this.dataForm.state = data.data.state
              this.dataForm.isDeleted = data.data.isDeleted
              this.dataForm.status = data.data.status
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.id = this.dataForm.id || undefined
      this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            SaveSignature(this.dataForm)
              .then((response) => {
                if (response && response.code === 20000) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                } else {
                  this.$message.error(data.msg)
                }
              })
          }
        }
      )
    },
    // 获取企业下拉框列表数据
    getFirmByJSFRole() {
      getFirmByJSFRole().then((response) => {
        this.firmInfos = response.data
      }).catch((err) => {
        this.$message({
          type: 'error',
          message: '查询所有建设方企业信息失败'
        })
      })
    }
  }
}
</script>
